<template>
	<view>
		<navbar :parameter='parameter'></navbar>
		<scroll-view scroll-x="true" class="scroll-h" :scroll-left="scrollleft" scroll-with-animation
			:style="{top:navH+'rpx'}">
			<block v-for="(item,index) in tabBars" :key="index">
				<view class="tab-item" :class="{'active' :state==index}" @tap="tabtap(item.id,index)">
					<view class="">{{item.name}}</view>
				</view>
			</block>
		</scroll-view>
		<view class="wap">
			<block v-for="(item,index) in orderList" :key='index' v-if="orderList.length > 0 ">
				<navigator class="order-item" :url="item.info_url+'?id='+item.id+'&accompanying='+1">
					<view class="title-line acea-row row-between-wrapper">
						<view class="num">订单号：{{item.order_sn}}</view>
						<view class="status" :class="item.status == 0?'orange':(item.status == 1?'blue':'grey')">
							{{item.status == 0?'待支付':(item.status == 1?'已支付':(item.status == 2?'已完成':'已取消'))}}
						</view>
					</view>
					<view class="line acea-row row-between-wrapper">
						<view class="title">{{item.hospital_name}}</view>
						<view class="price">￥{{item.pay_price}}</view>
					</view>
					<view class="type">{{item.product_name}}</view>
					<view class="time">陪诊时间： {{item.selected_date}} </view>
					<view class="order-bottom acea-row row-right">
						<view class="btn bg-blue" v-if="item.can_start == 1 " @click.stop="start(item.id)"> 开始订单 </view>
						<view class="btn bg-blue" v-if="item.can_stop == 1" @click.stop="stop_show(item.id)"> 结束订单 </view>
					</view>
				</navigator>
			</block>
			<u-empty mode="order" icon="http://cdn.uviewui.com/uview/empty/order.png" v-if="orderList.length == 0 ">
			</u-empty>
		</view>
		<u-loadmore :status="status" line dashed @loadmore="loadmore" v-if="orderList.length > 0" />
		<u-toast ref="uToast"></u-toast>
		<u-popup :show="show" @close="close" :round="10" mode="center">
			<view class="partner-box">
				<input type="text" v-model="confirm_code" placeholder="请输入服务确认码" placeholder-class="psc">
				<view @click="stop" class="btn">确定结束</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	let app = getApp();
	export default {
		data() {
			return {
				parameter: {
					'navbar': '1',
					'return': '1',
					'title': '我的订单',
					'color': true,
					'class': 'on'
				},
				state: 0,
				scrollleft: 0,
				tabBars: [{
					id: '',
					name: '全部'
				}, {
					id: 0,
					name: '待支付'
				}, {
					id: 1,
					name: '已支付'
				}, {
					id: 3,
					name: '已完成'
				}, {
					id: 4,
					name: '已取消'
				}],
				orderList: [],
				status: 'loadmore',
				order_status: '',
				limit: 10,
				page: 1,
				total_num: '',
				navH: app.globalData.navHeight,
				order_id: '',
				show: false,
				confirm_code:'',
			}
		},
		onLoad(option) {
			this.orderList = [];
			this.page = 1;
			this.state = Number(option.status) + 1;
			console.log(this.state)
			this.orderLists(option.status);
		},
		onShow() {

		},
		onReachBottom() {
			this.loadmore()
		},
		methods: {
			async orderLists(status) {
				const res = await this.$myRequest({
					url: '/api/manage/orderLists', //把接口传过去
					data: {
						page: this.page,
						limit: this.limit,
						status: status,
						openid: uni.getStorageSync('openid'),
					}
				})
				console.log(res.data)
				this.orderList = [...this.orderList, ...res.data.data];
				this.total_num = res.data.total_num;
			},
			async start(id) {
				var that = this;
				const res = await this.$myRequest({
					url: '/api/manage/startService', //把接口传过去
					data: {
						openid: uni.getStorageSync('openid'),
						order_id: id
					}
				})
				if (res.data.status == 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: '开始服务',
						complete() {
							that.page = 1;
							that.orderList = [];
							that.orderLists(that.order_status)
						}
					})
				}
			},
			async stop() {
				var that = this;
				const res = await this.$myRequest({
					url: '/api/manage/stopService', //把接口传过去
					data: {
						openid: uni.getStorageSync('openid'),
						order_id: that.order_id,
						confirm_code: that.confirm_code,
					}
				})
				if (res.data.status == 200) {
					this.$refs.uToast.show({
						type: 'success',
						message: '结束服务',
						complete() {
							that.show = false;
							that.confirm_code = "";
							that.page = 1;
							that.orderList = [];
							that.orderLists(this.order_status)
						}
					})
				}
			},
			stop_show(id) {
				this.order_id = id;
				this.show = true;
			},
			close() {
				this.show = false;
			},
			tabtap(id, index) {
				this.state = index;
				this.page = 1;
				this.order_status = id;
				this.orderList = [];
				this.orderLists(id);
			},
			loadmore() {
				console.log(11111)
				if (this.total_num == this.orderList.length) {
					this.status = 'nomore';
				} else {
					this.status = 'loading';
					this.page++;
					this.orderLists();
				}
				setTimeout(() => {
					if (this.total_num == this.orderList.length) this.status = 'nomore';
					else this.status = 'loading';
				}, 2000)
			}
		}
	}
</script>

<style style="scss" scoped>
	page {
		background-color: #F4F4F4;
	}

	.scroll-h {
		flex-direction: row;
		white-space: nowrap;
		background-color: #fff;
		position: fixed;
		width: 750rpx;
		left: 0;
		top: 0;

	}

	.tab-item {
		display: inline-block;
		flex-wrap: nowrap;
		padding-bottom: 24rpx;
		color: #333;
		font-size: 28rpx;
		text-align: center;
		position: relative;
		width: 150rpx;
	}

	.tab-item.active::after {
		content: "";
		position: absolute;
		left: 50%;
		bottom: 0;
		transform: translateX(-50%);
		background-color: #677afd;
		height: 6rpx;
		border-radius: 4rpx;
		width: 40rpx;
	}

	.wap {
		padding: 100rpx 30rpx 20rpx 30rpx;
	}

	.order-item {
		width: 100%;
		border-radius: 20rpx;
		background-color: #fff;
		margin-bottom: 30rpx;
		padding: 30rpx;
	}

	.order-item .title-line {
		padding-bottom: 20rpx;
		color: #666666;
		font-size: 26rpx;
		border-bottom: 1px solid #F4F4F4;
	}


	.order-item .title-line .num {
		width: 480rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		color: #595D68;
	}

	.order-item .title-line .status {
		color: #999999;
	}

	.order-item .title-line .status.orange {
		color: #FE8802;
	}

	.order-item .title-line .status.blue {
		color: #347DFE;
	}

	.order-item .line {
		color: #000;
		padding: 20rpx 0;
		font-size: 30rpx;
	}

	.order-item .line .number {
		color: #333333;
	}

	.order-item .type {
		color: #666;
		font-size: 26rpx;
		padding-bottom: 20rpx;
	}

	.order-item .time {
		color: #999;
		font-size: 24rpx;
	}

	.order-item .order-bottom {
		padding-top: 30rpx;
		font-size: 24rpx;
		color: #999999;

	}



	.order-item .order-bottom .price text {
		font-size: 36rpx;
		font-weight: 700;
	}

	.order-item .order-bottom .btn {
		border-radius: 30rpx;
		width: 150rpx;
		height: 54rpx;
		font-size: 26rpx;
		text-align: center;
		color: #000000;
		border: 1px solid #CBCBCB;
		margin-left: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}



	.order-item .order-bottom .btn.bg-blue {
		background-color: #347DFE;
		border: 1px solid #347DFE;
		color: #fff;
	}

	.partner-box {
		padding: 30rpx;
		width: 400rpx;
		border-radius: 20rpx;
		color: #333;

	}

	.partner-box input {
		width: 100%;
		height: 60rpx;
		border: 1px  solid #CBCBCB;
		padding-left: 20rpx;
	}

	.partner-box .btn {
		width: 100%;
		height: 60rpx;
		border-radius: 10rpx;
		line-height: 60rpx;
		text-align: center;
		background-color: #347DFE;
		color: #fff;
		font-size: 28rpx;
		margin-top: 30rpx;
	}
</style>
